<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @author CHUN Dany <chundany@gmail.com>
 */
class Banner extends Admin_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->model('mod_banner');
  }
  
  public function view()
  {
    $uriSegment = 4;
    $perPage = 15;  
    $this->data = $this->mod_banner->getBanner(uri($uriSegment), $perPage);
    pagination_config(site_url(module_controller('view')), $this->mod_banner->getBannerNum(), $perPage, $uriSegment); // base_url, total_num, per_page, segment
    
    $this->title  = 'Banner Management';
    $this->action = 'Banner Management';
    $this->btnActtion = 'new';
    $this->lblActtion = 'Add New';
  }
  
  public function add()
  {
    if($this->input->post())
    {
      $this->form_validation->set_rules('name', 'Name', 'trim|xss_clean|required|max_length[45]')
                            ->set_rules('url', 'URL', 'trim|xss_clean|required|max_length[255]')
                            ->set_rules('image', 'Image', 'trim|xss_clean|max_length[255]')
                            ->set_rules('publish', 'Publish Date', 'trim|xss_clean|required|max_length[20]')
                            ->set_rules('category', 'Category', 'trim|xss_clean|required|max_length[10]|numeric')
                            ->set_rules('zone', 'Zone', 'trim|xss_clean|required|max_length[10]|numeric')
                            ->set_rules('order', 'Order Nº', 'trim|xss_clean|max_length[255]')
                            ->set_rules('description', 'Description', 'trim|xss_clean|max_length[255]')
                            ->set_rules('disable', 'Disable', 'trim|xss_clean|max_length[1]|numeric');
        
      if ($this->form_validation->run() === TRUE)
      {
        $config['upload_path'] = UPLOAD_PATH . 'banners/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '512';
      
        $this->load->library('upload', $config);
    
        if($this->upload->do_upload('image'))
        {
          $image = $this->upload->data();
          $imageName = $image['file_name'];
            
          if($this->mod_banner->insert($imageName) > 0)
          {
            resize_image($imageName, 0, 0, $config['upload_path'], $config['upload_path']);
            resize_image($imageName, 75, 75, $config['upload_path'], $config['upload_path'].'75x75/');
            successMsg('Successfully add new banner!');
            redirect(module_controller('view'));
          }
          else
          {
            delete_file(UPLOAD_PATH . 'banners/' . $imageName);
            errorMsg('Could not add new banner, please try again!');
          }
        }
        else
        {
          if(!$this->upload->display_errors())
          {
            if($id = $this->mod_banner->insert())
            {              
              successMsg('Successfully add new banner!');
              redirect(module_controller('view'));
            }
            else
            {
              errorMsg('Could not add new banner, please try again!');
            }
          }
          else
          {
            errorMsg($this->upload->display_errors());
          }
        }
      }
    }
    $this->category = $this->mod_banner->getCategoryList();
    $this->zone = $this->mod_banner->getZoneList();
    
    $this->title  = 'Add New Banner';
    $this->action = 'Add New Banner';
  }
  
  public function edit()
  {
    $id  = $this->uri->segment(4);
    $row = $this->mod_banner->getBannerById($id);
    
    if($this->input->post())
    {
      $this->form_validation->set_rules('name', 'Name', 'trim|xss_clean|required|max_length[45]')
                            ->set_rules('url', 'URL', 'trim|xss_clean|required|max_length[255]')
                            ->set_rules('image', 'Image', 'trim|xss_clean|max_length[255]')
                            ->set_rules('publish', 'Publish Date', 'trim|xss_clean|required|max_length[20]')
                            ->set_rules('category', 'Category', 'trim|xss_clean|required|max_length[10]|numeric')
                            ->set_rules('zone', 'Zone', 'trim|xss_clean|required|max_length[10]|numeric')
                            ->set_rules('order', 'Order Nº', 'trim|xss_clean|max_length[255]')
                            ->set_rules('description', 'Description', 'trim|xss_clean|max_length[255]')
                            ->set_rules('disable', 'Disable', 'trim|xss_clean|max_length[1]|numeric');
      
      if ($this->form_validation->run() === TRUE)
      {
        $config['upload_path'] = UPLOAD_PATH . 'banners/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '512';
      
        $this->load->library('upload', $config);
    
        if($this->upload->do_upload('image'))
        {
          $image = $this->upload->data();
          $imageName = $image['file_name'];

          if($this->mod_banner->update($id, $imageName) > 0)
          {
            resize_image($imageName, 0, 0, $config['upload_path'], $config['upload_path']);
            resize_image($imageName, 75, 75, $config['upload_path'], $config['upload_path'].'75x75/');

            delete_file(UPLOAD_PATH . 'banners/' . $row[field('image')]);
            delete_file(UPLOAD_PATH . 'banners/75x75/' . $row[field('image')]);

            successMsg('Successfully edit banner!');
            redirect(module_controller('view'));
          }
          else
          {
            delete_file(UPLOAD_PATH . 'banners/' . $imageName);
            errorMsg('Could not edit banner, please try again!');
          }
        }
        else
        {
          if(!$this->upload->display_errors())
          {
            if($id = $this->mod_banner->update($id))
            {
              successMsg('Successfully edit banner!');
              redirect(module_controller('view'));
            }
            else
            {
              errorMsg('Could not edit banner, please try again!');
            }
          }
          else
          {
            errorMsg($this->upload->display_errors());
          }
        }
      }
    }
    $this->category = $this->mod_banner->getCategoryList();
    $this->zone = $this->mod_banner->getZoneList();
    $this->row = $row;
      
    $this->title  = 'Edit Banner';
    $this->action = 'Edit Banner';
  }
  
  public function delete()
  {
    $id = $this->uri->segment(4);
    
    if ($this->mod_banner->delete($id) > 0)
    {
      successMsg('Successfully delete!');
    }
    else
    {
      errorMsg('Could not delete, please try again!');
    }

    redirect(module_controller('view'));
  }
}